package user

import (
	"myserver/app/service/user_service"
	"myserver/app/types"
	pkgApp "myserver/pkg/app"
	"myserver/pkg/constant"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

type UserController struct{}

// 新建用户
func (e *UserController) CeateUser(c *gin.Context) {
	var (
		m   types.User
		app = pkgApp.Gin{C: c}
	)
	httpCode, _, err := pkgApp.BindAndValid(c, &m)

	if err != nil {
		app.Response(httpCode, err.Error(), nil)
		return
	}
	userServic := user_service.UserService{U: &m}
	err = userServic.CreateUser()
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}

	app.Response(http.StatusOK, constant.SUCCESS, "注册成功")
}

// 更新用户
func (UserController) UpDataUser(c *gin.Context) {
	var (
		app = pkgApp.Gin{C: c}
		ls  []types.TypeUser
	)
	err := c.ShouldBindJSON(&ls)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	users, err := pkgApp.ConvertDataByArr[types.User](ls)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	userServic := user_service.UserService{}
	err = userServic.UpDataUser(users)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, "更新成功")
}

type DellUserStruct struct {
	UuIds string `json:"uuIds" form:"uuIds" binding:"required"`
}

// 删除用户
func (UserController) DellUser(c *gin.Context) {
	var (
		m   DellUserStruct
		app = pkgApp.Gin{C: c}
	)
	httpCode, errCode, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, errCode, err.Error())
		return
	}
	uuids := strings.Split(m.UuIds, ",")
	userServic := user_service.UserService{}
	list, err := userServic.FindUserByUuid(uuids)
	if err != nil {
		app.Response(http.StatusInternalServerError, constant.FAIL_ADD_DATA, err)
		return
	}
	if len(list) == 0 {
		app.Response(http.StatusInternalServerError, "查询数据为空", nil)
		return
	}
	err = userServic.DellDatasByuuid(list)
	if err != nil {
		app.Response(http.StatusInternalServerError, constant.FAIL_ADD_DATA, err)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, nil)
}

type loginStruct struct {
	UserName string `json:"userName" form:"userName" binding:"required"`
	PassWord string `json:"passWord" form:"passWord" binding:"required"`
}

func (UserController) Login(c *gin.Context) {
	var (
		m   loginStruct
		app = pkgApp.Gin{C: c}
	)
	httpCode, errCode, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, errCode, err.Error())
		return
	}
	userServic := user_service.UserService{}
	token, err := userServic.Login(m.UserName, m.PassWord)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, token)

}

// 获取用户信息
func (UserController) GetUserInfo(c *gin.Context) {
	var (
		app = pkgApp.Gin{C: c}
	)
	user, _ := c.Get("user")
	app.Response(http.StatusOK, constant.SUCCESS, user)
}

func (UserController) GetUserList(c *gin.Context) {
	var (
		m        = types.UserListStruct{}
		app      = pkgApp.Gin{C: c}
		listData = types.UserListResponse{}
	)
	httpCode, errCode, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, errCode, err.Error())
		return
	}
	if m.Page == 0 {
		app.Response(http.StatusInternalServerError, "page不能为0", nil)
		return
	}
	if m.Size <= 0 {
		app.Response(http.StatusInternalServerError, "size必须大于0", nil)
		return
	}
	userServic := user_service.UserService{}
	ls, count, err := userServic.GetUserList(m.Page, m.Size, m.KeyWords)

	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	ls1, err := pkgApp.ConvertDataByArr[types.User](ls)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	listData.List = ls1
	listData.Total = count

	app.Response(http.StatusOK, constant.SUCCESS, listData)
}
